using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using UKVSTS;

namespace IEeee
{
    public partial class CreateBug : Form
    {
        public CreateBug ()
        {
            InitializeComponent ();
        }

        public WorkItem WorkItem
        {
            get { return workItemFormControl1.Item; }
            set { workItemFormControl1.Item = value; }
        }


        private void cmdSave_Click (object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty (workItemFormControl1.GetFirstFieldError ()) == false)
                {
                    workItemFormControl1.HandleFirstFieldError (workItemFormControl1.GetFirstFieldError ());
                }
                else
                {
                    Cursor oldCursor = Cursor.Current;
                    try
                    {
                        Cursor.Current = Cursors.WaitCursor;
                        workItemFormControl1.Item.Save ();
                    }
                    finally
                    {
                        Cursor.Current = oldCursor;
                    }
                    this.DialogResult = DialogResult.OK;
                    this.Close ();
                }
            }
            catch (Exception ex)
            {
                IEeeeException.DisplayError(this, ex);
            }
        }

        private void CreateBug_FormClosing (object sender, FormClosingEventArgs e)
        {

            if (DialogResult == DialogResult.OK)
            {
            }
            else if (DialogResult == DialogResult.Cancel)
            {
                DialogResult res = MessageBox.Show (this,
                                                    "The work item has been modified.\nDo you want to discard the changes?",
                                                    "Work Item",
                                                    MessageBoxButtons.OKCancel,
                                                    MessageBoxIcon.Exclamation,
                                                    MessageBoxDefaultButton.Button2);

                switch (res)
                {
                    case DialogResult.OK:
                        e.Cancel = false;
                        break;
                    case DialogResult.Cancel:
                        e.Cancel = true;
                        break;
                    default:
                        System.Diagnostics.Debug.Assert (false, "Unexpected DialogResult: " + res.ToString ());
                        break;
                }
            }
        }

        private void CreateBug_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            System.Diagnostics.Process.Start("http://www.codeplex.com/UKVSTS/Wiki/View.aspx?title=Bug%20Details");
        }

        private void cmdDefaultValues_Click(object sender, EventArgs e)
        {
            try
            {
                using (WiDefaultValues dialog = new WiDefaultValues())
                {
                    dialog.WorkItemType = IEeeeConfig.Instance.SelectedWorkItemType;
                    dialog.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {
                IEeeeException.DisplayError(this, ex);
            }
        }
    }
}